package br.com.ime.usp.cmep;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;

import com.markupartist.android.widget.ActionBar;
import com.markupartist.android.widget.ActionBar.AbstractAction;
import com.markupartist.android.widget.ActionBar.IntentAction;

import br.com.ime.usp.cmep.BandexUtils.BandexAccessor;
import br.com.ime.usp.cmep.BandexUtils.BandexComment;
import br.com.ime.usp.cmep.BandexUtils.BandexNetworkAccessor;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class SendRatingActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.perai);
		
		// actionbar
        final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
        actionBar.setTitle(getString(R.string.rating_titulo));
        actionBar.setHomeAction(new HomeAction(this));
		
		// enviando dados...
		Bundle b = getIntent().getExtras();
		String bandex = b.getString("bandex");
		String comentario = b.getString("comentario");
		String fila = b.getString("fila");
		
		new PostRatingTask().execute(bandex, comentario, fila);
	}
	
	protected void goBack() {
		Intent intent = new Intent(SendRatingActivity.this, RatingActivity.class);
		startActivity(intent);
		finish();
	}
	
	protected void goBack(String error) {
		Toast.makeText(getApplicationContext(), error, Toast.LENGTH_SHORT).show();
		goBack();
	}
	
	
	
	// ------ Async Task ---------
	
	private class PostRatingTask extends AsyncTask<String, Void, Void> {
		private String error = "";
		
		/** The system calls this to perform work in a worker thread and
	      * delivers it the parameters given to AsyncTask.execute() */
	    protected Void doInBackground(String... data) {
	        return postRating(data[0], data[1], data[2]);
	    }
	    
	    /** The system calls this to perform work in the UI thread and delivers
	      * the result from doInBackground() */
	    protected void onPostExecute(Void result) {
	    	if(error.equals(""))
	    		goBack();
	    	else
	    		goBack(error);
	    }
	    
	    // faz o post do Rating
	    private Void postRating(String bandex, String comentario, String fila) {
	    	try {
	    		BandexAccessor ba = new BandexNetworkAccessor();
	    		ba.postBandexComment(new BandexComment(bandex, comentario, fila));
	    	}
	    	catch(Exception e) {
	    		error = e.getMessage();
	    	}
			
			return null;
	    }
	}
	
	
	
	
	// --------- coisas da actionbar ---------------
	
	// nao faz nada
    private class HomeAction extends AbstractAction {
    	private Activity act;
        public HomeAction(Activity a) {
            super(R.drawable.ic_launcher); act = a;
        }
        public void performAction(View view) {
        }
    }
}